/*
 * 全局配置
 * @author li.xiaoxi
 * 
 * @date 2016-12-19
 */
var path = require('path')
var fs = require('fs')
var commonMethod = require('../util/method.js');

//用户配置
var cwd = process.cwd(),
    configPath = cwd+'/think.config.js',
    config = {};

//容错
try {
  config = require(configPath);

} catch(err){

  console.log('用户没有个性化配置文件think.config.js ~~');
}

//编译 beta环境的代码
global.build_beta = 'host';

//初始化创建项目 默认
var init_config = {
  app: 'app',
  //静态文件目录名
  html: 'html',
  //静态脚本资源目录名
  src: 'src',
  //styles脚本目录名
  styles: 'css',
  //scripts脚本目录名
  scripts: 'js',

  //打包目录
  zip: 'zip',
  static: 'static',
  cpPath: '',
  filePath: []
};

//默认配置
var defaults = {
  //本地服务器端口号
  port: '3000',
  //本地数据mock默认配置
  mock: {
    //目录名称
    directory: 'mock',
    //返回文件类型
    type: 'json',
    //url规则
    urlRule: /api|ajax|vgift/
  },

  //接口代理转发默认配置
  proxy: {
    host: 'http://test-mvip.immomo.com'
  },

  init: init_config,

  //webpack相关配置
  webpack: {
    hash: false,
    entryJsRule: init_config.src+'/**/**_entry.{js,jsx,vue}',
    htmlRule: init_config.html+'/**/**.{html,htm}',
    assetsRoot: cwd,
    assetsPublicPath: '/' + init_config.static + '/' + init_config.scripts + '/',
    assetsSubDirectory: init_config.static,
    filename: '[name].js',
    resolve: {
      extentions: ['', '.js', '.vue', '.json', '.html'],
      alias: {
      }
    },

    devtool: '#eval-source-map',

    dev: {
      //css是否支持map
      cssSourceMap: true,
      //css是否独立成文件
      extract: false,
      output: {
        path: path.resolve(cwd, init_config.static),
        filename: '[name].js',
        publicPath: '/' + init_config.static + '/' + init_config.scripts + '/',
      }
    },
    build: {
      //'css/[name].[contenthash].css'
      cssname: 'static/css/[name].css',
      htmlminify: false,
      //是否需要打包
      productionGzip: false,
      productionGzipExtensions: '',
      //css是否支持map
      //cssSourceMap: true,
      //css是否独立成文件
      extract: true,
      sourceMap: false,
      assetsRoot: cwd,
      assetsSubDirectory: init_config.static,
      output: {
        path: path.resolve(cwd, init_config.zip),
        //filename: '[name].js',
        publicPath: './',
        filename: path.posix.join(init_config.static, init_config.scripts, '/[name].js')
        //chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
      }
    }
  }
};

global.think_config = commonMethod.extend(true, {}, defaults, config);
